package cn.rubintry.bug.hunting.activity

import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import androidx.activity.addCallback
import androidx.activity.viewModels
import cn.rubintry.bug.common.base.BaseActivity
import cn.rubintry.bug.hunting.databinding.ActivityArticleDetailBinding
import cn.rubintry.bug.hunting.common.http.viewmodel.ArticleViewModel

import com.blankj.utilcode.util.ToastUtils
import com.just.agentweb.AgentWeb
import com.just.agentweb.AgentWebSettingsImpl
import com.just.agentweb.DefaultWebClient


class ArticleDetailActivity : BaseActivity<ActivityArticleDetailBinding>(), View.OnClickListener {

    private val articleViewModel: ArticleViewModel by viewModels<ArticleViewModel>()

    private var articleId = ""

    private var agentWeb : AgentWeb ? = null;


    override fun bindingClazz(): Class<ActivityArticleDetailBinding> {
        return ActivityArticleDetailBinding::class.java
    }

    override fun getActivityParams() {
        articleId = intent?.extras?.getString("articleId" , "") ?: ""
    }

    override fun initView() {
        binding?.imgBack?.setOnClickListener(this)
        onBackPressedDispatcher.addCallback {
            back()
        }
    }

    private fun back() {
        if(agentWeb?.webCreator?.webView?.canGoBack() == true){
            agentWeb?.webCreator?.webView?.goBack()
        }else{
            finish()
        }
    }

    override fun observeData() {
        articleViewModel.articleData.observe(this) { data ->
            if(data.code == "0"){
                binding?.tvArticleTitle?.text = data?.data?.title ?: ""
                articleViewModel.addToHistory(articleId , data?.data?.title ?: "" , data?.data?.sampleContent ?: "")
            }else{
                ToastUtils.showShort(data.message)
            }
        }
        agentWeb = AgentWeb.with(this)
            .setAgentWebParent(binding?.agentWeb, ViewGroup.LayoutParams(-1, -1))
            .useDefaultIndicator(Color.parseColor("#ff0d0d"))
            .setAgentWebWebSettings(AgentWebSettingsImpl.getInstance())
            .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
            .interceptUnkownUrl()
            .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
            .createAgentWeb()//
            .ready()//
            .go("https://rubintry.com.cn/article/${articleId}")

    }

    override fun onStart() {
        super.onStart()
        articleViewModel.getArticleById(articleId)
    }

    override fun onPause() {
        super.onPause()
        agentWeb?.webLifeCycle?.onPause()
    }

    override fun onResume() {
        super.onResume()
        agentWeb?.webLifeCycle?.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        agentWeb?.webLifeCycle?.onDestroy()
    }

    override fun onNetWorkError(msg: String?) {

    }

    override fun onClick(v: View?) {
        when(v){
            binding?.imgBack -> {
                back()
            }
        }
    }

}